#ifndef AVALON_DELIMITEDREADER_HPP
#define AVALON_DELIMITEDREADER_HPP
#include "Avalon/Base/Handle.hpp"
#include "Avalon/IO/Buffer.hpp"
#include "Avalon/IO/PipedMessageReader.hpp"
#include "Avalon/IO/PipedMessageWriter.hpp"

namespace Avalon {
namespace IO {

  /*! \class DelimitedReader
   *  \brief Reads character delimited data.
   */
  class DelimitedReader : public Reader {
    public:

      //! Constructs a DelimitedReader.
      /*!
        \param source The source to read from.
        \param delimiter The delimiter.
      */
      DelimitedReader(Carry<Reader> source, char delimiter);

      virtual ~DelimitedReader();

      virtual void Read(Buffer* destination,
        Out<Threading::Async<int> > sizeRead);

      virtual void Read(void* destination, int size,
        Out<Threading::Async<int> > sizeRead);

      virtual void Read(Buffer* destination, int size,
        Out<Threading::Async<int> > sizeRead);

    private:
      Handle<Reader> m_source;
      char m_delimiter;
      PipedMessageReader m_reader;
      PipedMessageWriter m_writer;
      int m_readChunkSize;
      Buffer m_destination;
      Buffer m_remainder;
      Threading::Async<int> m_sizeRead;

      void ReadSource();
      void OnRead();
  };
}
}

#endif // AVALON_DELIMITEDREADER_HPP
